package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ExhibitionDetailsModel;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.view.activity.imagedetails.ImageDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/3/8.
 */

public class ExhibitionDetailsAdapter extends RecyclerView.Adapter<ExhibitionDetailsAdapter.Holder> {

    private Context context;
    private List<ExhibitionDetailsModel.Guest> dataList;

    public ExhibitionDetailsAdapter(List<ExhibitionDetailsModel.Guest> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
    }

    public void refresh(List<ExhibitionDetailsModel.Guest> dataList) {
        this.dataList = new ArrayList<>();
        if (dataList != null) {
            this.dataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_exhibition_details, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final ExhibitionDetailsModel.Guest guest = dataList.get(position);
        //按照图片比例将图片宽放大/缩小到屏幕宽度
        Glide.with(context)
                .load(guest.getImage())
                .asBitmap()
                .error(R.drawable.im_default)
                .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        int imageWidth = resource.getWidth();
                        int imageHeight = resource.getHeight();
                        int screenWidth = PhoneUtils.getScreenWidth(context);
                        float scale = ((float) screenWidth) / imageWidth;
                        int height = (int) (scale * imageHeight);

                        ViewGroup.LayoutParams params = holder.imageView.getLayoutParams();
                        if (params == null) {
                            params = new ViewGroup.LayoutParams(screenWidth, height);
                        } else {
                            params.height = height;
                            params.width = screenWidth;
                        }
                        holder.imageView.setLayoutParams(params);
                        holder.imageView.setImageBitmap(resource);
                    }
                });
        holder.name.setText(guest.getGuestName());

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Rect rect = new Rect();
                holder.imageView.getGlobalVisibleRect(rect);
                ImageDetailsActivity.startDetailsActivity(context, guest.getImage(), rect);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.image)
        ImageView imageView;
        @Bind(R.id.name)
        TextView name;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
